<?php if( !defined( 'MAIN_DIR' ) ) { header( 'Location: ' . dirname( PHP_SELF ) . '/' ); exit( ); }
	$LANG = ( isset( $LANG ) && $LANG ? $LANG : null );
	
	include( SWAT_DIR . 'classes' . DS . 'File.class.php' );
	
	$file = new File( );
	$parent = new File( );
	
	if( isset( $_POST['file_create'] ) || isset( $_POST['file_create_x'] ) )	{
		$file->setParent( $_POST['file_id'] );
		$file->setName( $_POST['file_newName'] );
		$file->setFileType( $_POST['file_type'] );
		$file->commit( );
	}
	
	if( isset( $_POST['file_delete'] ) )	{
		$file->setIdFile( $_POST['file_delete'] );
		$file->erase( );
	}
	
	if( isset( $_POST['file_active'] ) && $_POST['file_active'] != '' && $_POST['file_file'] )	{
		$file->load( $_POST['file_file'] );
		$file->setActive( $_POST['file_active'] == '1' );
		$file->commit( );
	}
	
	if( isset( $_POST['file_pos'] ) && $_POST['file_pos'] && $_POST['file_parentId'] && $_POST['file_file'] )	{
		$pFile = new File( );
		$pFile->setIdFile( $_POST['file_file'] );
		$pFile->setParent( $_POST['file_parentId'] );
		$pFile->setPosition( $_POST['file_pos'] );
		$pFile->setValidPosition( );
	}
	
	if( isset( $_REQUEST['file_id'] ) && $_REQUEST['file_id'] )
		$file->load( $_REQUEST['file_id'] );
	if( !$file->getIdFile( ) && isset( $_REQUEST['file_parentId'] ) && $_REQUEST['file_parentId'] )
		$file->setParent( $_REQUEST['file_parentId'] );
	if( !$file->getIdFile( ) && !$file->load( 1 ) )	// Home file
		finalError( ' 2. ' . $LANG['E']['MAIN_FILE'] );
	
	if( $file->getParent( ) )
		$parent->load( $file->getParent( ) );
	else
		$parent = null;

	if( isset( $_POST['file_update'] ) || isset( $_POST['file_update_x'] ) )	{
		$file->setName( $_POST['file_name'] );
		$file->commit( );
	}
	
	$function = strtolower( ( isset( $_GET['file_function'] ) ? $_GET['file_function'] : ( $file->getFileType( ) ? $file->getFileType( ) : 'home' ) ) );

	if( isset( $_POST['file_updateProperties'] ) || isset( $_POST['file_updateProperties_x'] ) )	{
		$file->getProperties( );
		foreach( $_POST as $key => $value )	{
			if( strpos( $key, 'file_' ) !== 0 )
				$file->setProperty( $key, $value, Properties::TYPE_TEXT );
		}
		$file->commitProperties( );
	}
		
	if( isset( $_POST['file_propertyDelete'] ) && $_POST['file_propertyDelete'] )	{
		if( $file->getProperty( $_POST['file_propertyDelete'] ) && $file->getPropertyType( $_POST['file_propertyDelete'] ) == Properties::TYPE_TEXT )
			deleteUpload( $file->getProperty( $_POST['file_propertyDelete'] ) );
		$file->setProperty( $_POST['file_propertyDelete'], null, null );
		$file->commitProperties( );
		unset( $_POST );
	}
	
	$path = '';
	if( $parent )	{
		$parents_ids = explode( ',', trim( $file->getPath( ), ',' ) );
		$parents = $file->getPathNames( );
		$max = count( $parents );
		for( $c = 0; $c < $max; $c ++ )
			$path .= '/<a href="index.php?file_id=' . $parents_ids[$c] . '">' . $parents[$c] .'</a>';
	}
	$path .= '/<a href="index.php?file_id=' . $file->getIdFile( ) . '"><u>' . $file->getName( ) . '</u></a>';
	
	include( SWAT_DIR . 'pages' . DS . 'menu.php' );
	unset( $parents, $parents_ids, $max, $c, $path );
	
	if( cacheExists( $_SESSION['user']['name'] ) )
		cacheRead( $_SESSION['user']['name'] );
	else	{
		ob_start( );
		include( SWAT_DIR . 'pages' . DS . 'tree.php' );
		$content = ob_get_contents( );
		ob_end_flush( );
		cacheSet( $_SESSION['user']['name'], $content );
		unset( $content );
	}
?>
	<script language="javascript" type="text/javascript">
	//<![CDATA[
		<?php echo 'treeClick(' . str_replace( ',', ',true);treeClick(', $file->getIdFIle( ) . $file->getPath( ) . ');' ); ?>
		
		var elem = toObject( 'opt_<?php echo $file->getIdFIle( ); ?>' );
		if( elem )
			elem.className='treeDataSelected';
	//]]>
	</script>

<div id="fileHolder">
	<form name="file_form" id="file_form" action="index.php" method="post" enctype="multipart/form-data">
<?
	toTitle( toImage( $file ) . '&nbsp;<b>' . $file->getFileType( ) . ':</b> ' . $file->getName( ) );
	
	if( cacheExists( $file->getIdFile( ) . $file->getCreationDate( ) ) )
		cacheRead( $file->getIdFile( ) . $file->getCreationDate( ) );
	else	{
		ob_start( );
		include( SWAT_DIR . 'pages' . DS . 'filetypes' . DS . $function . '.php' );
		$content = ob_get_contents( );
		ob_end_flush( );
		cacheSet( $file->getIdFile( ) . $file->getCreationDate( ), $content );
		unset( $content );
	}
?>
	<input type="hidden" name="file_function" id="file_function" value="<?= $function ?>" />
	<input type="hidden" name="file_id" id="file_id" value="<?= $file->getIdFile( ) ?>" />
	<input type="hidden" name="file_file" id="file_file" value="" />
	<input type="hidden" name="file_propertyDelete" id="file_propertyDelete" value="" />
	<input type="hidden" name="file_delete" id="file_delete" value="" />
	<input type="hidden" name="file_active" id="file_active" value="" />
	<input type="hidden" name="file_pos" id="file_pos" value="" />
	<input type="hidden" name="file_parentId" id="file_parentId" value="<?= ( $parent ? $parent->getIdFile( ) : '' ) ?>" />
	</form>
</div>
